package game

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/game"
	gameReq "github.com/flipped-aurora/gin-vue-admin/server/model/game/request"
)

type GameZoneService struct{}

// CreateGameZone 创建gameZone表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameZoneService *GameZoneService) CreateGameZone(gameZone *game.GameZone) (err error) {
	err = global.GVA_DB.Create(gameZone).Error
	return err
}

// DeleteGameZone 删除gameZone表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameZoneService *GameZoneService) DeleteGameZone(ID string) (err error) {
	err = global.GVA_DB.Delete(&game.GameZone{}, "id = ?", ID).Error
	return err
}

// DeleteGameZoneByIds 批量删除gameZone表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameZoneService *GameZoneService) DeleteGameZoneByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]game.GameZone{}, "id in ?", IDs).Error
	return err
}

// UpdateGameZone 更新gameZone表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameZoneService *GameZoneService) UpdateGameZone(gameZone game.GameZone) (err error) {
	err = global.GVA_DB.Model(&game.GameZone{}).Where("id = ?", gameZone.ID).Updates(&gameZone).Error
	return err
}

// GetGameZone 根据ID获取gameZone表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameZoneService *GameZoneService) GetGameZone(ID string) (gameZone game.GameZone, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&gameZone).Error
	return
}

// GetGameZoneInfoList 分页获取gameZone表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameZoneService *GameZoneService) GetGameZoneInfoList(info gameReq.GameZoneSearch) (list []game.GameZone, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&game.GameZone{})
	var gameZones []game.GameZone
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&gameZones).Error
	return gameZones, total, err
}

// GetGameZoneInfoAll 获取gameZone表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameZoneService *GameZoneService) GetGameZoneInfoAll() (list []game.GameZone, err error) {
	// 创建db
	db := global.GVA_DB.Model(&game.GameZone{})
	var gameZones []game.GameZone
	err = db.Find(&gameZones).Error
	return gameZones, err
}
